﻿


using UnityEngine;
using System.Collections;
using System.Collections.Generic;
/// <summary>
/// Loading screen.
/// </summary>
using HLLib;
//using HLLIB_ASSETBUNDLE;
//using DataDefine;
//using WorldDefine;
/// <summary>
/// Loading screen.
/// </summary>
//using DataDefine;
//using DataDefine;


public class LoadingScreen : MonoBehaviour {



	public const int STATE_LOADING_OPEN_UI = 0; // create ui.
	public const int STATE_LOADING_ASEETBUNDLE_WITHOUT_SCENE = 1; // load assetbundle bytes without scene.
	public const int STATE_LOADING_ASSETBUDNLE_SYN_SCENE = 2; // load assetbundle bytes in scene.
	public const int STATE_LOAIDNG_COMPLETE = 3; // loading ending.


	/// <summary>
	/// The STAT e_ LOADIN g_ IMMEDIAT e_ CHANG e_ SCEN.
	/// </summary>
	public const int STATE_LOADING_IMMEDIATE_CHANGE_SCENE = 4; // IMMEIDATE SCENE.


	///
	public int state_loading = -1;
	public int all_process = 0;



	///
	public static LoadingScreen _instance = null;
	public GameObject _uiloading = null;


	


	/// <summary>
	/// Awake this instance.
	/// </summary>
	void Awake()
	{
	
	}


	// Use this for initialization
	void Start () {

		//if(null != SelectLevel._selectlevel)

		//_instance = this;

		//all_process = 1;
		//if(GameScreen.isOpenAssetBunlde){
		//	startLoadAssetbundle(STATE_LOADING_OPEN_UI);
		//	createLoadUI();
		//}else{
		//	startCommonChange(STATE_LOADING_OPEN_UI);
		//}

//		createLoadUI();
	}


	/// <summary>
	/// Starts the with out asset bundle.
	/// </summary>
	public void startCommonChange(int _loading_state)
	{
//		state_loading = _loading_state;
//		switch(state_loading)
//		{
//		case STATE_LOADING_OPEN_UI:
//		{
//			Debug.Log("---------- STATE_LOADING_OPEN_UI WITHOUT ASSETBUNDLE --------------");
//			Debug.Log("loading: curAssetBundleConfigName: " + GameScreen.curSceneName);
//			all_process = 20;
//			Debug.Log("---------- STATE_LOADING_OPEN_UI WITHOUT ASSETBUNDLE ENDING --------------");
//			this.startCommonChange(STATE_LOADING_IMMEDIATE_CHANGE_SCENE);
//			break;
//		}
//		case STATE_LOADING_IMMEDIATE_CHANGE_SCENE:
//		{
//			Debug.Log("---------- STATE_LOADING_IMMEDIATE_CHANGE_SCENE --------------");
//			all_process = 70;
//			Application.LoadLevel(GameScreen.curSceneName);
//			all_process = 100;

//			Debug.Log("---------- STATE_LOADING_IMMEDIATE_CHANGE_SCENE ENDING--------------");
//			this.startCommonChange(STATE_LOAIDNG_COMPLETE);
//			break;
//		}
//		case STATE_LOAIDNG_COMPLETE:
//		{
//			/// Add Scene Script:
//			AddSceneScript(GameScreen.curAddScriptName);
//			Debug.Log("<<<<<<<<<<<<<AddSceneScript: " + GameScreen.curAddScriptName + ">>>>>>>>>>>>>>");
//			/// DELETE UI:
//			deleteScriptLoadingScreen();
////			deleteLoadUI();
//			Debug.Log("---------- STATE_LOAIDNG_COMMOND_COMPLETE SUCESS!!! -------------");
//			break;
//		}
//		}
	}


	/// <summary>
	/// Starts the load assetbundle.
	/// </summary>
	public void startLoadAssetbundle(int _loading_state)
	{
//		state_loading = _loading_state;
//		switch(state_loading)
//		{
//		case STATE_LOADING_OPEN_UI:
//		{
//			Debug.Log("---------- STATE_LOADING_OPEN_UI --------------");
//			Debug.Log("loading: curAssetBundleConfigName: " + GameScreen.curAssetBundleConfigName);
//			Debug.Log("loading: curAssetBundleSceneName: " + GameScreen.curAssetBundleSceneName);
//			all_process = 20;
//			/// create:

//			///
//			this.startLoadAssetbundle(STATE_LOADING_ASEETBUNDLE_WITHOUT_SCENE);
//			Debug.Log("---------- STATE_LOADING_OPEN_UI ENDING --------------");
//			break;
//		}
//		case STATE_LOADING_ASEETBUNDLE_WITHOUT_SCENE:
//		{
//			Debug.Log("---------- STATE_LOADING_ASEETBUNDLE_WITHOUT_SCENE --------------");
//			///  Deletes all asset bundle info and clear gameobject cache.
//			HLResourcesCacheManager.getInstance().DeleteAllAssetBundleInfoAndClearGameObjectinCache();
//			all_process = 25;
////			AssetbundleManager.loadingSynAsssetBundleNum = 0;	
//			AssetbundleManager.instance.LoadAssetbundleBytesWithDatatable(GameScreen.curAssetBundleConfigName);
////			// "Configdatas/Level001ConfigData"
//			all_process = 75;
//			Debug.Log("---------- STATE_LOADING_ASEETBUNDLE_WITHOUT_SCENE ENDING--------------");
//			break;
//		}
//		case STATE_LOADING_ASSETBUDNLE_SYN_SCENE:
//		{
//			Debug.Log("---------- STATE_LOADING_ASSETBUDNLE_SYN_SCENE --------------"+GameScreen.curAssetBundleSceneName);
//			this.StopCoroutine("assetbundle_syn_scene");
//			this.StartCoroutine(assetbundle_syn_scene ());
//			all_process = 90;
//			Debug.Log("---------- STATE_LOADING_ASSETBUDNLE_SYN_SCENE ENDING --------------");
////			/// TODO Check Error: 
////			List<LevelAssetBundleConfigData> _list_levelConfigData = DatasManager.getDataInstance().list_levelAssetBundleConfigData;
////			if(null != _list_levelConfigData)
////			{
////				for(int i = 0;i < _list_levelConfigData.Count;i++)
////				{
////					Debug.LogError("Load Asssetbundle Error: " + _list_levelConfigData[i].files_path + " ,id: " + _list_levelConfigData[i].id);
////				}
////			}
//			break;
//		}
//		case STATE_LOAIDNG_COMPLETE:
//		{
////			Debug.Log("---------- STATE_LOAIDNG_COMPLETE SUCESS--------------");
//			/// Add Scene Script:
//			AddSceneScript(GameScreen.curAddScriptName);
////			Debug.Log("<<<<<<<<<<<<<AddSceneScript: " + GameScreen.curAddScriptName + ">>>>>>>>>>>>>>");
//			/// DELETE UI:
//			deleteScriptLoadingScreen();
////			deleteLoadUI();
////			Debug.Log("---------- STATE_LOAIDNG_COMPLETE ENDING --------------");
//			break;
//		}
//		}
	}


	void AddSceneScript(string _script_name)
	{
		//if(_script_name.Equals("ScreenMain"))
		//{
		//	GameScreen.SharedGameScreenManager()._GlobalObj.AddComponent<ScreenMain>();
		//}
		//else if(_script_name.Equals("ScreenCover"))
		//{
		//	GameScreen.SharedGameScreenManager()._GlobalObj.AddComponent<ScreenCover>();
		//}
		//else if(_script_name.Equals("ScreenLogo"))
		//{
		//	GameScreen.SharedGameScreenManager()._GlobalObj.AddComponent<ScreenLogo>();
		//}
		//else if(_script_name.Equals("ScreenSelect"))
		//{
		//	GameScreen.SharedGameScreenManager()._GlobalObj.AddComponent<ScreenSelect>();
		//}
		//else if(_script_name.Equals("ScreenPlatform"))
		//{
		//	GameScreen.SharedGameScreenManager()._GlobalObj.AddComponent<ScreenPlatform>();
		//}
	}

	
	
	// Update is called once per frame
	void Update () 
	{

//		switch(state_loading)
//		{
//		case STATE_LOADING_OPEN_UI: // 
//		{

//			break;
//		}
//		case STATE_LOADING_ASEETBUNDLE_WITHOUT_SCENE:// assetbundle
//		{
//			List<LevelAssetBundleConfigData> _list_levelConfigData = DatasManager.getDataInstance().list_levelAssetBundleConfigData;
////			if(null == _list_levelConfigData)
////			{
////				startLoadAssetbundle(STATE_LOADING_ASSETBUDNLE_SYN_SCENE);
////				return;
////			}
				
//			///
//			if(null == _list_levelConfigData)
//			{
//				startLoadAssetbundle(STATE_LOADING_ASSETBUDNLE_SYN_SCENE);
//				return;
//			}
//			///
//			else
//			{
//				if( _list_levelConfigData.Count <= 0) //if( AssetbundleManager.loadingSynAsssetBundleNum <= 0)
//				{
//					startLoadAssetbundle(STATE_LOADING_ASSETBUDNLE_SYN_SCENE);
//				}
//				else
//				{
//					//Debug.Log("AssetbundleManager.loadingSynAsssetBundleNum: " + AssetbundleManager.loadingSynAsssetBundleNum);
//					Debug.Log("STATE_LOADING_ASEETBUNDLE_WITHOUT_SCENE waitting !!!!");
//					//				Debug.Log("_list_levelConfigData.Count : "  + _list_levelConfigData.Count);
//					//				for(int i = 0; i < _list_levelConfigData.Count;i++)
//					//				{
//					//					Debug.Log(_list_levelConfigData[i].files_path);
//					//				}
//					//				Debug.Log("------------------------------------------------------");
//				}
//			}
//			break;
//		}
//		case STATE_LOADING_ASSETBUDNLE_SYN_SCENE: // assetbundle
//		{

//			if(AssetbunderLoad.async !=null )
//			{
//				float _loading_scene_process = AssetbundleManager.instance.getSynChnageScenesProcess(AssetbunderLoad.async);

//				if(_loading_scene_process == 100)
//				{

//						Debug.Log("_loading_scene_process: " + _loading_scene_process);
//						AssetbunderLoad.async.allowSceneActivation = true;
//						AssetbunderLoad.async=null;
//				}
//			}

//			if( GameScreen.curAssetBundleSceneName.EndsWith (Application.loadedLevelName) )
//			{
//				/// close ui:

//				all_process = 100;
//				/// COMPLETE:
//				startLoadAssetbundle(STATE_LOAIDNG_COMPLETE);
//			}
//			break;
//		}
//		case STATE_LOAIDNG_COMPLETE:
//		{

//			break;
//		}
//		}
//		///
////		if(AssetbunderLoad.async!=null)
////		{
////			float _loading_scene_process = AssetbundleManager.instance.getSynChnageScenesProcess(AssetbunderLoad.async);
////			if(_loading_scene_process==100)
////			{
////				AssetbunderLoad.async.allowSceneActivation = true;
////				AssetbunderLoad.async = null;
////			}
////		}

	}


	/// <summary>
	/// Deletes the script loading screen.
	/// </summary>
	public void deleteScriptLoadingScreen()
	{
//		if(null != GameScreen.SharedGameScreenManager()._LoadingScreen)
//		{
//			Destroy(GameScreen.SharedGameScreenManager()._LoadingScreen);
//			GameScreen.SharedGameScreenManager()._LoadingScreen = null;

//			//destroy loading ui
////         删除 load 界面
////			UISprite[] _allTra = _uiloading.transform.GetComponentsInChildren<UISprite>();
////			for (int i = 0; i < _allTra.Length; i++) {
////				Destroy(_allTra[i].gameObject);
////				_allTra[i] = null;
////			}
////
////			Destroy(_uiloading);
////			_uiloading = null;

//			Resources.UnloadUnusedAssets();
//			System.GC.Collect();
//		}
	}


	/// <summary>
	/// Creates the load UI
	/// </summary>
	public void createLoadUI(){

//		_uiloading = Instantiate(Resources.Load("UISystem/UILoading")) as GameObject;
//		if(null != _uiloading){
//			_uiloading.transform.parent = UIManager.instance.Lowet.transform;
//			_uiloading.transform.localScale = Vector3.one;
//		}else{
//			return;
//		}

	}
	


//	public void deleteLoadUI(){
//
//		if(_uiloading != null){
//			Debug.Log ("-----0000-----------------------");
//			GameObject _uiobj = UIManager.instance.Lowet.transform.FindChild("UILoading(clone)").gameObject;
//			Destroy(_uiobj);
//			_uiobj = null;
//			_uiloading = null;
//		}
//	}


	/// <summary>
	/// Loads the single game object.
	/// </summary>
	/// <returns>The single game object.</returns>
	//static public IEnumerator assetbundle_syn_scene ()
	//{
////		yield return new WaitForSeconds(0.1f);
//		for(int i = 2; --i>=0; )
//			yield return new WaitForEndOfFrame();
//		AssetbundleManager.instance.AssetAsyncChangeScene(GameScreen.curAssetBundleSceneName);
////		while(true)
////		{
////			Debug.Log("Application.loadedLevelName: " + Application.loadedLevelName);
////		}
	//}

	
	/// <summary>
	/// Raises the GU event.
	/// </summary>
	Rect _rect = new Rect(400,300,300,100);
	public void OnGUI()
	{
		GUI.Label(_rect,"LOADING LOADING LOADING 9999999!!!!!!");
	}




}
